// pages/xinjian/monkey/monkey.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		xb: 0
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		wx.getStorage({
			key: 'uid',
			success: (e) => {
				this.setData({
					uid: e.data
				})
				wx.request({
					url: getApp().globalData.xc,
					method: "POST",
					header: {
						"Content-Type": "application/x-www-form-urlencoded"
					},
					data: {
						HTTP_API: 'vv/usercenter/api/user/profile',
						token: e.data.uid
					},
					success: (e) => {
						console.log(e)
						this.setData({
							data: e.data.data.userinfo
						})
					}
				})
				wx.request({
					url: getApp().globalData.xc,
					method: "POST",
					header: {
						"Content-Type": "application/x-www-form-urlencoded"
					},
					data: {
						HTTP_API: 'vv/money/api/index/rechargePackage',
						token: e.data.uid
					},
					success: (e) => {
						console.log(e)
						this.setData({
							num: e.data.data
						})
					}
				})
			}
		})
	},
	xb(e) {
		console.log(e)
		this.setData({
			xb: e.currentTarget.dataset.id
		})
	},
	sure() {
		wx.getStorage({
			key:'user',
			success: (res) => {
				wx.request({
					url: getApp().globalData.xc,
					method: "POST",
					header: {
						"Content-Type": "application/x-www-form-urlencoded"
					},
					data: {
						HTTP_API: 'vv/sync/api/index/user',
						user_id:this.data.uid.uid,
						login_token:this.data.uid.uid,
						miniapp_openid:res.data.openId
					},
					success:(en)=>{
						wx.request({
							url: getApp().globalData.xc,
							method: "POST",
							header: {
								"Content-Type": "application/x-www-form-urlencoded"
							},
							data: {
								HTTP_API: 'vv/money/api/index/chongzhi',
								token: this.data.uid.uid,
								memo: '',
								money: this.data.num[this.data.xb].chongMoney,
								taocan_id: this.data.num[this.data.xb].taocanId
							},
							success: (e) => {
								console.log(e)
								wx.request({
									url: getApp().globalData.xc,
									method: "POST",
									header: {
										"Content-Type": "application/x-www-form-urlencoded"
									},
									data: {
										HTTP_API: 'vv/weixinpay/api/index/pay',
										token: this.data.uid.uid,
										money:e.data.data.money,
										trade_no:e.data.data.paylog,
										trade_type:"JSAPP"
									},
									success: (e) => {
										console.log(e)
										wx.requestPayment({
											'timeStamp': e.data.data.timeStamp,
											'nonceStr': e.data.data.nonceStr,
											'package': e.data.data.package,
											'signType': e.data.data.signType,
											'paySign': e.data.data.paySign,
											'success': function(res) {
												wx.navigateBack({
													delta:1
												})
											},
											'fail': function(res) {
												console.log(res)
											},
										})
									}
								})
							}
						})
					}
				})
				
			}
		})
		
	}
})
